package com.cn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.api.ApiCode;
import com.cn.api.ApiResult;
import com.cn.mapper.ShopCommodityMapper;
import com.cn.mapper.ShopCtcMapper;
import com.cn.pojo.shop.ShopCommodity;
import com.cn.pojo.shop.ShopCtc;
import com.cn.pojo.vo.ShopCateCtcVo;
import com.cn.service.ShopCommentService;
import com.cn.service.ShopCommodityService;
import com.cn.service.ShopCtcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 28631
 * @description 针对表【shop_ctc(类目)】的数据库操作Service实现
 * @createDate 2024-02-29 15:12:20
 */
@Service
public class ShopCtcServiceImpl  extends ServiceImpl<ShopCtcMapper, ShopCtc>
        implements ShopCtcService {

    @Autowired
    private ShopCtcMapper shopCtcMapper;

    @Autowired
    private ShopCommodityMapper shopCommodityMapper;

    /**
     * 查询类目信息
     * @return
     */
    @Override
    public ApiResult queryCtc() {
        List<ShopCateCtcVo> shopCtcs = shopCtcMapper.queryCtc();
        return ApiResult.success(shopCtcs);
    }

    /**
     * 修改
     * @param shopCtc 类目
     * @param id 分类id
     * @return
     */
    @Override
    public ApiResult updateCtc(ShopCtc shopCtc,Integer id) {
        if (id != null && id!=0) {
            shopCtc.setCateId(id);
        }
        int i = shopCtcMapper.updateById(shopCtc);
        if (i>0) {
            return ApiResult.success(ApiCode.SUCCESS);
        }
        return ApiResult.error(ApiCode.SYSTEM_ERROR.getCode(), ApiCode.SYSTEM_ERROR.getMsg());
    }

    /**
     * 删除数据信息
     * @param id
     * @return
     */
    @Override
    public ApiResult deleteCtc(Integer id) {
        //查询是否存在下级
        ShopCtc shopCtc = shopCtcMapper.selectById(id);
        LambdaQueryWrapper<ShopCommodity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopCommodity::getCtcId,shopCtc.getId());
        List<ShopCommodity> shopCommodities = shopCommodityMapper.selectList(queryWrapper);
        if (shopCommodities != null) {
            return ApiResult.error(ApiCode.NOT_USER.getCode(), ApiCode.NOT_USER.getMsg());
        }
        int i = shopCtcMapper.deleteById(id);
        return ApiResult.success(ApiCode.SUCCESS);

    }

    /**
     * 查询数据
     * @return
     */
    @Override
    public ApiResult querySelectCtc() {
        return ApiResult.success(shopCtcMapper.querySelectCtc());
    }
}




